package com.movie.service.impl;

import com.movie.entity.Rating;
import com.movie.mapper.RatingMapper;
import com.movie.service.RatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RatingServiceImpl implements RatingService {

    @Autowired
    private RatingMapper ratingMapper;

    @Override
    public int addOrUpdateRating(Rating rating) {
        Rating existing = ratingMapper.findByUserAndMovie(rating.getUserId(), rating.getMovieId());
        if (existing != null) {
            existing.setScore(rating.getScore());
            existing.setComment(rating.getComment());
            return ratingMapper.updateRating(existing);
        } else {
            return ratingMapper.insertRating(rating);
        }
    }

    @Override
    public Rating getUserRating(Integer userId, Integer movieId) {
        return ratingMapper.findByUserAndMovie(userId, movieId);
    }

    @Override
    public List<Rating> getRatingsByMovie(Integer movieId) {
        return ratingMapper.findByMovieId(movieId);
    }

    @Override
    public List<Rating> getRatingsByUser(Integer userId) {
        return ratingMapper.findByUserId(userId);
    }
}
